package com.you.trans.xa.service.impl;

import com.you.trans.xa.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class XATransferServiceServiceImpl implements TransferService {
    @Autowired
    @Qualifier("jamesJdbcTemplate")
    private JdbcTemplate jamesJdbcTemplate;

    @Autowired
    @Qualifier("peterJdbcTemplate")
    private JdbcTemplate peterJdbcTemplate;

    @Transactional
    public String transfer(int money) {
        int resultJames = jamesJdbcTemplate.update("update bank_a set money = money - ? where user_name = ?",money,
                "james");
        try {
            //Thread.sleep(1000 * 60);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        int resultPeter = peterJdbcTemplate.update("update bank_b set money = money + ? where user_name = ?",money,
                "peter");
        if (money > 20){
            throw new RuntimeException("money too large");//系统宕机了怎么办？
        }
        return resultJames+"-"+resultPeter;
    }

}
